/***************************************************
 ** @Desc : This file for ...全局方法
 ** @Time : 2018-8-27 18:05 
 ** @Author : Joker
 ** @File : global
 ** @Last Modified by : Joker
 ** @Last Modified time: 2018-8-27 18:05
 ** @Software: GoLand
****************************************************/
package utils

import (
	"bytes"
	"time"
	"golang.org/x/text/transform"
	"golang.org/x/text/encoding/simplifiedchinese"
	"io/ioutil"
	"crypto/md5"
	"encoding/hex"
	"math/rand"
	"strconv"
	"strings"
)

type GlobalMethod struct{}

/*返回当前时间的字符串*/
func (*GlobalMethod) GetNowTime() string {
	return time.Now().Format("2006-01-02 15:04:05")
}

/*返回当前时间的字符串*/
func (*GlobalMethod) GetNowTimeV2() string {
	return time.Now().Format("20060102150405")
}

/*gbk转为utf-8*/
func (*GlobalMethod) GbkToUtf8(s []byte) ([]byte, error) {
	reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
	data, e := ioutil.ReadAll(reader)
	if e != nil {
		return nil, e
	}
	return data, nil
}

/*
	返回当前操作数据库的状态
	返回Json串
	// 9 success
	// -9 fail
*/
func (*GlobalMethod) JsonFormat(code int, data interface{}, msg string, url string) (json map[string]interface{}) {
	if code == SUCCESS_FLAG {
		json = map[string]interface{}{
			"code": code,
			"data": data,
			"msg":  msg,
			"url":  url,
		}
	} else {
		json = map[string]interface{}{
			"code": code,
			"msg":  msg,
			"url":  url,
		}
	}
	return json
}

/*返回当前操作数据库的状态:成功/失败*/
func (*GlobalMethod) GetDatabaseStatus(code int) map[string]interface{} {
	msg := FAILED_STRING
	if code == SUCCESS_FLAG {
		msg = SUCCESS_STRING
	}
	out := make(map[string]interface{})
	out["code"] = code
	out["msg"] = msg
	return out
}

/*建立请求,以表单HTML形式构造*/
func (*GlobalMethod) BuilderRequest(rParams map[string]string, action, method, btnName string) (html string) {
	var buf bytes.Buffer
	//表头
	buf.WriteString(`<form id="rppaysubmit" name="rppaysubmit" action="` + action + `" method="` + method + `" >`)
	//内容
	for k, v := range rParams {
		buf.WriteString(`<input type="hidden" name="` + k + `" value="` + v + `"/><br>`)
	}
	//提交
	buf.WriteString(`<input type="submit" value="` + btnName + `" style="display: none"/></form>`)
	buf.WriteString("<script>document.forms['rppaysubmit'].submit();</script>")
	return buf.String()
}

/*在数字、大写字母、小写字母范围内生成num位的随机字符串*/
func (*GlobalMethod) RandomString(length int) string {
	// 48 ~ 57 数字
	// 65 ~ 90 A ~ Z
	// 97 ~ 122 a ~ z
	// 一共62个字符，在0~61进行随机，小于10时，在数字范围随机，
	// 小于36在大写范围内随机，其他在小写范围随机
	rand.Seed(time.Now().UnixNano())
	result := make([]string, 0, length)
	for i := 0; i < length; i++ {
		t := rand.Intn(62)
		if t < 10 {
			result = append(result, strconv.Itoa(rand.Intn(10)))
		} else if t < 36 {
			result = append(result, string(rand.Intn(26)+65))
		} else {
			result = append(result, string(rand.Intn(26)+97))
		}
	}
	return strings.Join(result, "")
}

/*
	注册密码加盐
	//返回hash和盐
*/
func (this *GlobalMethod) PasswordSolt(pwd string) (string, string) {
	salt := this.RandomString(32)
	m5 := md5.New()
	m5.Write([]byte(pwd))
	m5.Write([]byte(salt))
	st := m5.Sum(nil)
	return hex.EncodeToString(st), salt
}

/*
	登录密码加盐
	//返回hash
*/
func (*GlobalMethod) LoginPasswordSolt(pwd, salt string) string {
	m5 := md5.New()
	m5.Write([]byte(pwd))
	m5.Write([]byte(salt))
	st := m5.Sum(nil)
	return hex.EncodeToString(st)
}
